/*
 * Licensed to Nanjing RedOrange Co. ltd (RedOrange) under one or more
 * contributor license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * RedOrange licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */
package redora.generator;

/**
 * Archetype Template without content. Gives hints to the ModelProcessor what to
 * do with this template.
 * 
 * See also templates.xsd.
 * 
 * @author jaap
 */
public class Template {

	public Template(String name, Type type,
			Destination destination) {
		super();
		this.name = name;
		this.type = type;
		this.destination = destination;
	}

	public enum Type {
		freemarker, xslt, copy
	}

	public enum Destination {
		source, target
	}

	/** File type of the destination. */
	public enum DestinationType {
		java, xml, html, css, properties
	}

	/** Data input for the template. Normally it is the Model files, but there are different options. */
	public enum Input {
		/** The standard model file. */
		Model,
		/** Bundle of all model files.*/
		AllModels,
		/** List of all the languaes used in the model files. */
		Language, 
		/** All global scoped enums. */
		Enum
	}

	public String name;
	public String packageSuffix;
	public void setPackageSuffix(String packageSuffix) {
		this.packageSuffix = packageSuffix;
	}

	public String path;
	public void setPath(String path) {
		this.path = path;
	}

	public String ignoreProjects;

	public void setIgnoreProjects(String ignoreProjects) {
		this.ignoreProjects = ignoreProjects;
	}

	public Type type;
	public Destination destination;
	public DestinationType destinationType;
	public void setDestinationType(DestinationType destinationType) {
		this.destinationType = destinationType;
	}

	public Input input;

	public void setInput(Input input) {
		this.input = input;
	}

	private String destinationFileName;
	private String templateFileName;

	public void setTemplateFileName(String templateFileName) {
		this.templateFileName = templateFileName;
	}

	public String getDestinationFileName(String objectName, String language,
			String classAttribute) {
		if (destinationFileName == null) {
			destinationFileName = name;
			if (objectName != null) {
				destinationFileName = destinationFileName.replace(
						"${objectName}", objectName);
			}
			if (language != null) {
				destinationFileName = destinationFileName.replace(
						"${language}", language);
			}
			if (classAttribute != null) {
				destinationFileName = destinationFileName.replace(
						"${classAttribute}", classAttribute);
			}

			if (destinationType != null) {
				destinationFileName = destinationFileName + "."
						+ destinationType;
			}
		}
		return destinationFileName;
	}

	public String getTemplateFileName() {
		if (templateFileName == null) {
			templateFileName = name.replace("${objectName}", "Pojo");

			if (type == Type.freemarker) {
				templateFileName = templateFileName + ".ftl";
			} else if (type == Type.xslt) {
				templateFileName = templateFileName + ".xsl";
			}
		}
		return templateFileName;
	}
}
